<!--
 * @Descripttion:
 * @version:
 * @Author: cxguo
 * @Date: 2019-10-30 21:29:17
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-07 13:35:58
 -->
<template>
  <div>
    <!-- dataObj: {{ dataObj }} -->
    <div class="clearfix">
      <h3 class="fl">商品信息</h3>
    </div>
    <el-divider />
    <el-form
      ref="goodBase"
      :model="dataObj"
      :label-position="'right'"
      :rules="ruleValidator"
      label-width="60px"
    >
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="编号:" prop="code">
            <el-input v-model="dataObj.code" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称:" prop="name">
            <el-input v-model="dataObj.name" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="分类:" prop="catId">
            <CatTreeSelect v-model="dataObj.catId" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-dialog top="20px" title="请选择分类:" :visible.sync="dialogVisible" append-to-body>
      <CateTree ref="cateTree" @node-click="onNodeClick" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script>
import { oftenuse } from '@/libs/mixins'
import CateTree from '@/views/bsd/goods/category'
import { countByCode } from '@/api/goods/goods-sku.js'
import clonedeep from 'clonedeep'
import { getGoodSkuCode } from '@/api/goods/goods-sku.js'
import CatTreeSelect from '@/views/components/GoodsCatTreeSelect'

export default {
  name: 'GoodBase',
  components: { CateTree, CatTreeSelect },
  mixins: [oftenuse],
  props: {
    multi: {
      type: Object,
      default: () => { return {} }
    },
    oper: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      iframe: {},
      dialogVisible: false,
      originObj: {},
      optionsData: {
        unit: []
      },
      ruleValidator: {
        code: [
          { required: true, validator: (rule, value, callback) => {
            if (value === '' || value === null || value === undefined) {
              return callback(new Error('编号不能为空！'))
            }
            if (this.oper === 'edit') {
              if (value === this.originObj.code) return callback()
            }
            countByCode(value).then(res => {
              if (res.data.flag) {
                const data = res.data.data
                if (data > 0) return callback(new Error('该编号已存在'))
                callback()
              }
              callback()
            })
          }, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ],
        catId: [{ required: true, message: '请选择所属类别', trigger: 'change' }],
        catName: [{ required: true, message: '请选择所属类别', trigger: 'change' }]
      }
    }
  },
  watch: {
  },
  created() {
    this.initData()
  },
  methods: {
    // 对外api
    setData(data) {
      this.dataObj = data
      this.originObj = clonedeep(data)
      if (this.oper !== 'edit') {
        this.genSkuCode()
      }
    },
    setNameBySpecs(specs) {
      const name = this.dataObj.name
      if (!name) return
      const skuName = name.split('-')[0]
      const result = `${skuName}${specs}`
      this.$set(this.dataObj, 'name', result)
    },
    getData() {
      return this.dataObj
    },
    btnSelectType() {
      this.dialogVisible = true
    },
    // API
    resetData() {
      this.dataObj = {}
      this.$set(this.multi, 'specs', false)
      this.$set(this.multi, 'unit', false)
    },
    initData() {
    },
    genSkuCode() {
      getGoodSkuCode().then(res => {
        if (!res.data.flag) return
        const code = res.data.data
        this.$set(this.dataObj, 'code', code)
      })
    },
    onNodeClick(data) {
      const { id, children, text } = data
      if (!children) {
        this.dialogVisible = false
        this.$set(this.dataObj, 'catId', id)
        this.$set(this.dataObj, 'catName', text)
      }
    }
  }
}
</script>
